package com.mytrip;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;

public class Test{
	public static void main(String[] args){
		t2();
	}

	private static void t2(){
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("D:/111.txt"), "UTF-8"));
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:/222.txt"), "UTF-8"));
			String str = null;
			while ((str = br.readLine()) != null) {
				String[] a = str.split("\\s+");
				bw.write("update `mytrip`.`city` set lat='"+a[2]+"',lon='"+a[3]+"' where name like '"+(!a[1].endsWith("市") ? a[1] : a[1].substring(0, a[1].length() - 1))+"%';");
				bw.newLine();
			}
			bw.close();
			br.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private static void t1(){
		try {
			SAXReader reader = new SAXReader();

			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("UTF-8");// 设置XML文件的编码格式

			String filePath = "e:\\weather_city.xml";
			File file = new File(filePath);
			StringBuilder sb = new StringBuilder(128);
			if (file.exists()) {
				Document document = reader.read(file);// 读取XML文件
				Element root = document.getRootElement();// 得到根节点

				String provinceName = null;
				String provinceNameEn = null;
				for (Iterator i = root.elementIterator("area"); i.hasNext();) {
					Element area = (Element) i.next();
					provinceName = area.attributeValue("name");
					provinceNameEn = area.attributeValue("en").toLowerCase();

					for (Iterator j = area.elementIterator("city"); j.hasNext();) {
						Element city = (Element) j.next();
						String id = city.attributeValue("id");
						if (id != null && !"".equals(id)) {
							String cityName = city.attributeValue("name");
							String cityNameEn = city.attributeValue("en").toLowerCase();
							sb.append("insert into city(name,province,name_py,province_py,weather_id) values('"
									+ cityName + "','" + provinceName + "','" + cityNameEn + "','" + provinceNameEn
									+ "','" + id + "');");
							sb.append("\n");
						}
					}
				}
			}
			System.out.println(sb.toString());

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
